<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="google-site-verification" content="xBT4GhYoi5qRD5tr338pgPM5OWHHIDR6mNg1a3euekI" />
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="智红的博客">
    <meta name="keyword"  content="智红">
    <link rel="shortcut icon" href="/img/favicon.ico">

    <title>
        
        C语言语法笔记 - 智红的博客
        
    </title>

    <!-- Custom CSS -->
    
<link rel="stylesheet" href="/css/aircloud.css">

    
<link rel="stylesheet" href="/css/gitment.css">

    <!--<link rel="stylesheet" href="https://imsun.github.io/gitment/style/default.css">-->
    <link href="//at.alicdn.com/t/font_620856_pl6z7sid89qkt9.css" rel="stylesheet" type="text/css">
    <!-- ga & ba script hoook -->
    <script></script>
<meta name="generator" content="Hexo 5.3.0"></head>

<body>

<div class="site-nav-toggle" id="site-nav-toggle">
    <button>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
        <span class="btn-bar"></span>
    </button>
</div>

<div class="index-about">
    <i> 好记性不如烂键盘呐 </i>
</div>

<div class="index-container">
    
    <div class="index-left">
        
<div class="nav" id="nav">
    <div class="avatar-name">
        <div class="avatar ">
            <img src="/img/avatar.jpg" />
        </div>
        <div class="name">
            <i>Zhihong Li</i>
        </div>
    </div>
    <div class="contents" id="nav-content">
        <ul>
            <li >
                <a href="/">
                    <i class="iconfont icon-shouye1"></i>
                    <span>主页</span>
                </a>
            </li>
            <li >
                <a href="/tags">
                    <i class="iconfont icon-biaoqian1"></i>
                    <span>标签</span>
                </a>
            </li>
            <li >
                <a href="/archive">
                    <i class="iconfont icon-guidang2"></i>
                    <span>存档</span>
                </a>
            </li>
            <li >
                <a href="/about/">
                    <i class="iconfont icon-guanyu2"></i>
                    <span>关于</span>
                </a>
            </li>
            
            <li>
                <a id="search">
                    <i class="iconfont icon-sousuo1"></i>
                    <span>搜索</span>
                </a>
            </li>
            
        </ul>
    </div>
    
        <div id="toc" class="toc-article">
    <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%93%E5%8D%B0%E9%99%90%E5%AE%9A%E9%95%BF%E5%BA%A6%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">打印限定长度字符串</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%89%93%E5%8D%B0%E5%90%8E%E5%9B%9E%E5%A1%AB%E9%95%BF%E5%BA%A6%E8%87%B3%E5%8F%82%E6%95%B0%E2%80%99-n%E2%80%99"><span class="toc-text">打印后回填长度至参数’%n’</span></a></li></ol>
</div>
    
</div>


<div class="search-field" id="search-field">
    <div class="search-container">
        <div class="search-input">
            <span id="esc-search"> <i class="icon-fanhui iconfont"></i></span>
            <input id="search-input"/>
            <span id="begin-search">搜索</span>
        </div>
        <div class="search-result-container" id="search-result-container">

        </div>
    </div>
</div>

        <div class="index-about-mobile">
            <i> 好记性不如烂键盘呐 </i>
        </div>
    </div>
    
    <div class="index-middle">
        <!-- Main Content -->
        


<div class="post-container">
    <div class="post-title">
        C语言语法笔记
    </div>

    <div class="post-meta">
        <span class="attr">发布于：<span>2020-12-09 11:40:37</span></span>
        
        <span class="attr">标签：/
        
        <a class="tag" href="/tags/#C/C++" title="C/C++">C/C++</a>
        <span>/</span>
        
        
        </span>
        <span class="attr">访问：<span id="busuanzi_value_page_pv"></span>
</span>
</span>
    </div>
    <div class="post-content no-indent">
        <h2 id="打印限定长度字符串"><a href="#打印限定长度字符串" class="headerlink" title="打印限定长度字符串"></a><a target="_blank" rel="noopener" href="https://rayed.com/posts/2016/02/limit-string-length-in-printf-in-c/">打印限定长度字符串</a></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> *s = <span class="string">&quot;This my world.&quot;</span>;</span><br><span class="line"><span class="built_in">printf</span>(<span class="string">&quot;Hello, %.5s!\n&quot;</span>, s+<span class="number">8</span>); <span class="built_in">printf</span>(<span class="string">&quot;Hello, %.*s!\n&quot;</span>, <span class="number">5</span>, s+<span class="number">8</span>);</span><br><span class="line"><span class="keyword">int</span> minlen=<span class="number">5</span>,maxlen=<span class="number">5</span>; <span class="built_in">printf</span>(<span class="string">&quot;Hello, %*.*s\n&quot;</span>, minlen, maxlen, s+<span class="number">8</span>);</span><br></pre></td></tr></table></figure>
<h2 id="打印后回填长度至参数’-n’"><a href="#打印后回填长度至参数’-n’" class="headerlink" title="打印后回填长度至参数’%n’"></a><a target="_blank" rel="noopener" href="https://www.geeksforgeeks.org/c-program-to-print-the-length-of-a-string-using-n-format-specifier/">打印后回填长度至参数’%n’</a></h2><figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">char</span> *str = <span class="string">&quot;world!&quot;</span>; <span class="keyword">int</span> len = <span class="number">0</span>; <span class="built_in">printf</span>(<span class="string">&quot;Hello %s\n%n&quot;</span>, str, &amp;len); <span class="built_in">printf</span>(<span class="string">&quot; = %d\n&quot;</span>, len); <span class="comment">// = 13</span></span><br></pre></td></tr></table></figure>
<p>##<br><a target="_blank" rel="noopener" href="https://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf">打印double格式’%f’</a></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">float</span> f = <span class="number">1.4</span>; <span class="built_in">printf</span>(<span class="string">&quot;%f&quot;</span>, f);  <span class="comment">//正确,f会被提升到double再用%f打印</span></span><br><span class="line"><span class="keyword">double</span> d = <span class="number">1.4</span>; <span class="built_in">printf</span>(<span class="string">&quot;%f&quot;</span>, d); <span class="comment">//正确,若指定&quot;%lf&quot;,则l无意义</span></span><br></pre></td></tr></table></figure>
        
        <br />
        <div id="comment-container">
        </div>
        <div id="disqus_thread"></div>

        <div id="lv-container">
        </div>

    </div>
</div>

    </div>
</div>


<footer class="footer">
    <ul class="list-inline text-center">
        
        

        

        

        

        

    </ul>
    
    <p>
        <span id="busuanzi_container_site_pv">
            <span id="busuanzi_value_site_pv"></span>PV
        </span>
        <span id="busuanzi_container_site_uv">
            <span id="busuanzi_value_site_uv"></span>UV
        </span>
        Created By <a target="_blank" rel="noopener" href="https://hexo.io/">Hexo</a>  Theme <a target="_blank" rel="noopener" href="https://github.com/aircloud/hexo-theme-aircloud">AirCloud</a></p>
</footer>




</body>

<script>
    // We expose some of the variables needed by the front end
    window.hexo_search_path = "search.json"
    window.hexo_root = "/"
    window.isPost = true
</script>
<script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script>

<script src="/js/index.js"></script>

<script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




</html>
